/**
 * Created by Administrator on 2017/8/7.
 */


var max_page,cur_page,cur_page_index,pre_page,margin_px;
var $window_w,$window_h,$pdf_left_navbar,$fwr_page,$fwr_page_wrap,fwr_page_len,$fwr_page_bg_image_h,$fwr_page_bg_image_w,$fwr_page_w,f_div_w;
var $jspPane_scroll,$select_on_small_img,small_img_con_h,$skeletonize_list;
var isSort = false;
var scroll_lf_h=0;
var small_reflash=true;
var lastPage; // 最后页数
var canvas_width;
var canvas_height;

//关于水印的初始化设置
var waterMarkState=$('#pdfchange').val();//没有生产水印的图片，需要用canvas生成
var waterMark=$('#watermark').val();//获取水印文字内容
//var waterMark='这是是水印内容';
//alert(waterMark);
var size_scale=1;
//var waterMark="这个是logo";
var markFontSize='30px 微软雅黑';
// 总页数
var pageCount= $("#pageCount").val();
// 上一状态显示的图片下标
var preId1=0, preId2=1, preId3=2;


//$('.size_scale a').click(function () {
//    size_scale = $(this).text();
//    var canvas_width_now=canvas_width*size_scale;
//    var canvas_height_now=canvas_height*size_scale;
//    canvas.width=canvas_width_now;
//    canvas.height=canvas_height_now;
//    if(size_scale==0.5){
//        markFontSize='26px 微软雅黑';
//    }else if(size_scale==1.5){
//        markFontSize='46px 微软雅黑';
//    }else{
//        markFontSize='36px 微软雅黑';
//    }
//
//    //重新生成图片
//    drawImg("canvas","timg.jpg",canvas_width_now,canvas_height_now,size_scale,markFontSize,waterMark);
//});


 $(window).load(function () {
 	//drag();
    $(".fwr_toobar_handtool").addClass("fwr_toobar_handtool_active");    
     //alert($fwr_page_bg_image_h);
    if(waterMarkState==2){
    	firstCanvasLoad();
    }
 });


    var endpage=$("#endpage").val();
    var $MainFrame = $("#MainFrame");
    var $fxtree_panel = $("#fxtree_panel");
    var $left_panel1_title = $("#left_panel1_title");
    var $pdf_left_nav = $("#pdf_left_nav");
    // 左侧菜单栏
    $pdf_left_navbar = $("#pdf_left_navbar");
    var $pdf_scroll_pane = $("#pdf_scroll_pane");
    var $jspContainer = $("#jspContainer");
    // 适合宽度的按钮
    var $btnFitWidth = $("#btnFitWidth");
    $jspPane = $("#jspPane");
    $jspPane_scroll=$("#jspPane_scroll");
    // 窗口大小
    $window_w = $(window).width();
    $window_h = $(window).height();
    /*火狐下获取窗口的高度*/
    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){  
     $window_h = window.innerHeight;
  	}
    //alert($window_h);
    var pdf_left_nav_width = $pdf_left_nav.width() + $fxtree_panel.width();
    var pdf_left_nav_height = $window_h - 46;
    var $fwr_page_bg_image = $(".fwr_page_bg_image");
    

    /*获取生存的psf图片的原始宽高*/
    $fwr_page_bg_image_w = $("#pagewidth").val();
    $fwr_page_bg_image_h = parseInt($("#pageheight").val());
    //console.log("pagewidth: "+$fwr_page_bg_image_w+"pageheight: "+$fwr_page_bg_image_h);
    //alert($fwr_page_bg_image_w+"  *****  "+$fwr_page_bg_image_h);
    
    // 左侧缩略图div
    $small_img=$(".skeletonize-img-con");
    small_img_con_h=179;
    // 缩小的按钮
    var $btnzoomOut = $("#btnzoomOut");
    // 放大的按钮
    var $btnzoomIn = $("#btnzoomIn");
    // 选择显示的百分比
    var $zoomSel = $("#zoomSel");
    // 小滚动条的限制con
    $skeletonize_list = $("#skeletonize_list");
    $skeletonize_list.height(pdf_left_nav_height - 14 - 40);
    // 获取缩略图div
    //$skeletonize_img_con = $(".skeletonize-img-con");


    // 给最外层祖div加高度
    $MainFrame.height($window_h);
    // 给左侧菜单增加高度
    $pdf_left_navbar.height(pdf_left_nav_height);

    // 初始化给右侧内容增加宽度高度
    var rt_con_w = $window_w - $pdf_left_nav.width();
    $jspContainer.width(rt_con_w);
    $jspContainer.height(pdf_left_nav_height);
    $pdf_scroll_pane.width(rt_con_w);
    $pdf_scroll_pane.height(pdf_left_nav_height);
    $jspPane.width(rt_con_w);
    $jspPane.height(pdf_left_nav_height);
    $fxtree_panel.height(pdf_left_nav_height);

    // 动态获取滚动条高度并且根据高度控制按钮显示隐藏以及第几页操作
    max_page = $("#sum").val(); //最大页数
    cur_page = 1; // 当前页
    cur_page_index = 0; // 当前页下标
    pre_page = 1; // 之前页
    margin_px = 14; // 页之间的距离
    $text_page_num=$("#textpagenum");
    
   
    $fwr_page = $(".fwr_page");
    $fwr_page_wrap = $(".fwr_page_wrap");
    fwr_page_len = $fwr_page.size();
    lastPage=fwr_page_len;
    f_div_w = $window_w - $pdf_left_navbar.width();
    $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
    
    //加载完页面以后调用函数执行
    setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
    
    
    //滚动滚动条
    $jspPane.scroll(function () {
   		 var $this =$(this);
          var  scrollTop =$(this).scrollTop();//滚动高度
          //console.log('comming')
           var pagenow=(scrollTop/($fwr_page_bg_image_h+margin_px))+1;
			pagenow = parseFloat(pagenow).toFixed(1) + "";
			var numArr = pagenow.split(".");
			var x=parseInt(numArr [0]);
			var y=parseInt(numArr [1]);
			if(y>6){
				x=x+1;
			}
			if(cur_page != x){
				pre_page = cur_page;
				cur_page=x;
				cur_page_index = cur_page - 1;
				sortPage();
			}
           
           
           //console.log("scrollTop:"+scrollTop+"fwr_page_bg_image_h: "+$fwr_page_bg_image_h+" cur_page(x): "+cur_page);

        // 控制首页前一页按钮的显示隐藏
        // 第一页
        if (cur_page == 1) {
            $("#btnfirst").addClass("toobar-btn-first-dis");
            $("#btnpre").addClass("toobar-btn-pre-dis");
        } else {
            //alert(2)
            $("#btnfirst").removeClass("toobar-btn-first-dis");
            $("#btnpre").removeClass("toobar-btn-pre-dis");
        }
        // 控制后页末页按钮的显示隐藏
        if (cur_page == max_page) {
            $("#btnnext").addClass("toobar-btn-next-dis");
            $("#btnlast").addClass("toobar-btn-last-dis");
        } else {
            $("#btnnext").removeClass("toobar-btn-next-dis");
            $("#btnlast").removeClass("toobar-btn-last-dis");
        }

        // 分页
        //alert("滚动里面的："+cur_page)
        $text_page_num.val(cur_page);

//         左侧选中随着右侧滚动而选中
        var skeletonize_img_con=$(".skeletonize-img-con");
        $select_on_small_img=skeletonize_img_con.eq(cur_page-1);
        $select_on_small_img.addClass("skeletonize-img-con-on").siblings().removeClass("skeletonize-img-con-on");
        skeletonize_img_con.find("img").removeClass("skeletonize-img-on");
        $select_on_small_img.find("img").addClass("skeletonize-img-on");
    });

   

 

    // 点击缩小按钮
    var selectVal = parseInt($zoomSel.find("option:selected").html().split("%")[0]);
    var select_index = $zoomSel.find("option:selected").val();
    var selectVal_before = selectVal;
    var initImageWidth = $fwr_page_bg_image_w; // 100%时图片的宽度
    //alert(multiple);

    // 点击缩小按钮
    $btnzoomOut.click(function () {
        if (select_index > 0) {
            select_index--;
            //alert(select_index);
            $("#zoomSel").val(select_index);
            $("#zoomSel").change();
        } else {
            alert(" 已经是最小");
        }
    });

    // 点击放大图片
    $btnzoomIn.click(function () {
    	
        if (select_index < 4) {
        	//alert('放大');
            select_index++;
            $("#zoomSel").val(select_index);
            $("#zoomSel").change();
        } else {
            alert(" 已经是最大");
        }
    });

    $zoomSel.change(function () {
        select_index = $(this).val();
        //alert(select_index);
        changeSize();
    })

    function changeSize() {
		//alert(fwr_page_len+"**");
		$fwr_page=$('.fwr_page');//重新找fwr_page这个类?为什么不能直接用$fwr_page
        if (select_index == 3) {
        	
            // 适合宽度的图片并且使得图片居中
            selectVal = parseInt(rt_con_w * 0.97 / initImageWidth * 100); // 适合宽度的相对于100%的百分比
            
            var multi = (rt_con_w * 0.97 / $fwr_page_bg_image_w);
            //alert(multi);
            $fwr_page_bg_image_w = rt_con_w * 0.97;
           // alert($fwr_page_bg_image_w);
            $fwr_page_bg_image_h = $fwr_page_bg_image_h * multi;
            $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
            
            //alert(selectVal);
           
            markFontSize='36px 微软雅黑';

            setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
            $btnFitWidth.addClass("toolbar-fitwidth-on");
        } else {
            selectVal = parseInt($zoomSel.find("option:selected").html().split("%")[0]);
            if(selectVal==50){
                markFontSize='26px 微软雅黑';
            }else if(size_scale==100){
            	markFontSize='36px 微软雅黑';
            }else{
            	 markFontSize='46px 微软雅黑';
            }           

            var incMultiple = selectVal / selectVal_before;
                       
            // 放大图片并且使得图片居中
            $fwr_page_bg_image_w = $fwr_page_bg_image_w * incMultiple;
            $fwr_page_bg_image_h = $fwr_page_bg_image_h * incMultiple;
            $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
            setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
            $btnFitWidth.removeClass("toolbar-fitwidth-on");
        }
        selectVal_before = selectVal;
        
        //alert("end:  "+selectVal_before);
        // 同时改变select的选中值
        $zoomSel.find("option").eq(select_index).prop("selected", true);
        omg(cur_page_index,1);
        $jspPane.scrollTop((cur_page - 1) * $fwr_page_bg_image_h + (cur_page - 2) * margin_px);
    }

    // 点击下一页
    $("#btnnext").click(function () {
        if(cur_page >= max_page) {
            //alert("已经是最后页!");
            return;
        }
        
        cur_page++;
        cur_page_index = cur_page - 1;
        //alert(cur_page+"%%%%%");
        sortPage();//第一页的话下标应该为cur_page_index, 即0
        $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
    });

    // 点击最后页
    $("#btnlast").click(function () {
        cur_page = max_page;//max_page位总页数，所以传入最后一页的话下标应该为max_page-1，在这边也就是cur_page-1
        cur_page_index = cur_page - 1;
        sortPage();
        $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
    });

    // 点击上一页
    $("#btnpre").click(function () {
        if(cur_page <= 1) {
            // alert("已经是首页!");
            return;
        }
        cur_page--;
        cur_page_index = cur_page - 1;
        //alert(cur_page+"***");
        sortPage();
        $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
    });

    // 点击首页
    $("#btnfirst").click(function () {
        cur_page = 1;
        cur_page_index = cur_page - 1;
        sortPage();
        $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
    });

    // 点击适合宽度的按钮
    $btnFitWidth.click(function () {
        select_index = 3;
        changeSize();
    });

    // 点击展开缩略图

    $("#btnList li").click(function () {
    	
        var $index = $(this).index();
        // 控制显示隐藏
        $fxtree_panel.show();
        if ($index == 0 || $index == 1) {
        	var pdf_path_normal =$('#sm_img_url').val();
        	//alert(pdf_path_normal);     
        	var len=parseInt($('#endpage').val());
        	var start=parseInt($('#startpage').val())-1;
        	var list_html="";
        	var sm_img_con_class="";
        	var sm_img="";
        	if(small_reflash==true){
        		for(var i=start;i<len;i++){
        			var sm_img_url="store/"+pdf_path_normal+"/small"+i+".jpg";
        			sm_img='<img src="'+sm_img_url+'" id="skeletonize_img_"'+i+' />';
        			sm_img_con_class="skeletonize-img-con";
        			if(i==start){
        				sm_img_con_class="skeletonize-img-con skeletonize-img-con-on"
        				sm_img='<img src="'+sm_img_url+'" id="skeletonize_img_"'+i+' class="skeletonize-img-on" />'	
        			}
        			var str='<div id="skeletonize_img_con_'+i+'" class="'+sm_img_con_class+'" onclick="smallImgSelected(this)">'
		                           +'<div id="skeletonize_img_box_'+i+'" class="skeletonize-img-box ">'
			                       +sm_img	                             
		                           +'</div>'
		                           +'<span id="skeletonize_page_'+i+'">'+(i+1)+'</span>'
		                       +'</div>'
		            list_html+=str;
        		}
        		$('#skeletonize_scroll').html(list_html);
        		 small_reflash=false;
        	}
            $(".pubDiv").hide();
            //alert("进来了");
            $(".pubDiv").eq(0).show();
            $(".left_panel").removeClass("left_panel-on");
            if ($index == 0) {
                $(this).next().addClass("left_panel-on");
            } else {
                $(this).addClass("left_panel-on");
            }
            $left_panel1_title.find("span").html("缩略图");
            $("#skeletonize_list").scrollTop(scroll_lf_h);//控制左边缩略图小滚动条定位
        } else if ($index == 2) {
            $(".pubDiv").hide();
            $(".pubDiv").eq(1).show();
            $(".left_panel").removeClass("left_panel-on");
            $(this).addClass("left_panel-on");
            $left_panel1_title.find("span").html("书签");
        }
       
        // 控制展开的宽度
        $pdf_left_navbar.width(pdf_left_nav_width);
        // 控制右侧内容的宽度和位置
        rt_con_w = $window_w - pdf_left_nav_width;
        //alert(rt_con_w)
        $pdf_scroll_pane.css({
            "width": rt_con_w + "px",
            "left": pdf_left_nav_width + "px"
        });
        $jspContainer.css({
            "width": rt_con_w + "px"
        });
        $jspPane.css({
            "width": rt_con_w + "px"
        });
        $jspPane_scroll.css({
            "width": rt_con_w + "px"
        });
        $fwr_page.css({
            "width": rt_con_w + "px"
        });


        // 展开缩略图以后重新计算图片的margin-left
        f_div_w = $window_w - pdf_left_nav_width;
        $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
        setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);

        changeSize();
    });
  

    // 点击收起缩略图

    $("#pac_up_btn").click(function () {
        $fxtree_panel.hide();
        $pdf_left_navbar.width($pdf_left_nav.width());
        // 控制右侧内容的宽度和位置
        rt_con_w = $window_w - $pdf_left_nav.width();
        $pdf_scroll_pane.css({
            "width": rt_con_w + "px",
            "left": $pdf_left_nav.width() + "px"
        });
        $jspContainer.css({
            "width": rt_con_w + "px"
        });
        $jspPane.css({
            "width": rt_con_w + "px"
        });
        $jspPane_scroll.css({
            "width": rt_con_w + "px"
        });
        $fwr_page.css({
            "width": rt_con_w + "px"
        });

        //alert(select_index);
        if (select_index == 3) {
            // 适合宽度的图片并且使得图片居中
            var multi = (rt_con_w * 0.97 / $fwr_page_bg_image_w);
            // alert(multi);
            $fwr_page_bg_image_w = rt_con_w * 0.97;
            $fwr_page_bg_image_h = $fwr_page_bg_image_h * multi;
            $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
            setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
            $btnFitWidth.addClass("toolbar-fitwidth-on");
        }

        // 收起缩略图以后重新计算图片的margin-left
        f_div_w = $window_w - $pdf_left_nav.width();
        $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
        setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);

        changeSize();
    });


    function setImageWidth(obj, page_len, img_w, img_h, img_margin_lf) {
    	//alert(page_len);
        /*for (var i = 0; i < page_len; i++) {
            var $this = obj.eq(i);
            var $fwr_page_bg_image = $this.find(".fwr_page_bg_image");
	             $this.css({
	             "width": img_w + "px",
	            "height": img_h + "px",
	           "margin-left": img_margin_lf + "px"
            });

            $this.find(".fwr_page_wrap").css({
                "width": img_w + "px",
                "height": img_h + "px"
            });
            $this.find(".loadingBg").css({
                "width": img_w + "px",
                "height": img_h + "px"
            });
            
            if(waterMarkState==2){
                canvas_width=img_w;
                canvas_height=img_h;
            }
         
            
        }*/
    	
        var $fwr_page_bg_image = obj.find(".fwr_page_bg_image");
        obj.css({
             "width": img_w + "px",
            "height": img_h + "px",
           "margin-left": img_margin_lf + "px"
        });

        obj.find(".fwr_page_wrap").css({
            "width": img_w + "px",
            "height": img_h + "px"
        });
        obj.find(".loadingBg").css({
            "width": img_w + "px",
            "height": img_h + "px"
        });
        
        if(waterMarkState==2){
            canvas_width=img_w;
            canvas_height=img_h;
        }
    }


    


//在页数输入框按回车跳转
function EnterPress(e){ //传入 event
    var e = e || window.event;
    if(e.keyCode == 13){   
    	cur_page=$text_page_num.val();
    	cur_page_index = cur_page - 1;
    	//alert(this_page);
    	sortPage();//传入第n页则下标为n-1
    	$jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
    }
}    

//点击左侧缩略图 选中,右侧滚动显示对应的大图
    function smallImgSelected(obj){
        $(obj).addClass("skeletonize-img-con-on").siblings().removeClass("skeletonize-img-con-on");
        var $this_img_sm = $(obj).find("img");
        $(obj).siblings().find("img").removeClass("skeletonize-img-on");
        $this_img_sm.addClass("skeletonize-img-on");
        cur_page=parseInt($(obj).index())+1;
        cur_page_index = cur_page - 1;
         sortPage();
         //console.log("缩略图 ： "+cur_page);
         $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));//控制大滚动条，定位到对应大图位置
    };
    
function labelPage(labelPage){//从书签跳转过来因为带的参数不是下标，所以要先减1再调用sortPage
	//alert(labelPage);
	var startpage=$("#startpage").val();
	var labelPage;
	//if(labelPage==0)labelPage=1;
	if(startpage==1){
		 labelPage=labelPage;
	}else{
		 labelPage=labelPage-startpage+1;
	}
	//alert(labelPage);
	cur_page = labelPage;
	cur_page_index = cur_page - 1;
	 sortPage();
	 //console.log(labelPage+"*****************");
	 $jspPane.scrollTop((labelPage)*($fwr_page_bg_image_h+margin_px));//控制大滚动条，定位到对应大图位置
}    
function sortPage(){
	//console.log(page+"*****************");
    // 点击左侧缩略图右侧滚动显示对应的大图
    $text_page_num.val(cur_page);
    
    //console.log("page:  " + cur_page_index);
    omg(cur_page_index); 
    //alert((cur_page-1)*small_img_con_h+14);
    scroll_lf_h=(cur_page_index)*small_img_con_h+14;
    $("#skeletonize_list").scrollTop(scroll_lf_h);//控制左边缩略图小滚动条定位
}


 
function omg(currentPageIndex, type){
	var fid=$("#fid").val();
	$.ajax({
       url: 'jumpServlet?page='+currentPageIndex+"&fid="+fid,
       type: 'get',
       datatype: "json",
       success: function (ret) {  
    	   var time=100;
    	   if(waterMarkState==1){
    		   time = 1000;
    	   }
    	   setTimeout(function(){ 
	         var obj = eval('(' + ret + ')');
	         var list = obj.list;
	         var len=list.length;
	         // 设置图片
        	 for(var i=0;i<len;i++){
        		 // 只有当图片未显示时才设置3,4,5
        		 if(type == 1 || (list[i].id != preId1 && list[i].id != preId2 && list[i].id != preId3)){
        			 //console.log("setImgid: "+list[i].id);
        			 
        			 setImg(parseInt(list[i].id), list[i].src)
        		 }
             }
        	 var reset1=true, reset2=true, reset3=true;
        	 // 判断preId1是否在当前页里面
        	 for(var i=0;i<len;i++){
        		 if(list[i].id == preId1){
        			 reset1 = false;
        			 break;
        		 }
             }
        	// 判断preId2是否在当前页里面
        	 for(var i=0;i<len;i++){
        		 if(list[i].id == preId2){
        			 reset2 = false;
        			 break;
        		 }
             }
        	// 判断preId3是否在当前页里面
        	 for(var i=0;i<len;i++){
        		 if(list[i].id == preId3){
        			 reset3 = false;
        			 break;
        		 }
             }
        	 // 当上一状态图片不在当前状态时，清空
        	 if(reset1){
        		 $('#loadingBg'+preId1).html("");
        	 }
        	 if(reset2){
        		 $('#loadingBg'+preId2).html("");
        	 }
        	 if(reset3){
        		 $('#loadingBg'+preId3).html("");
        	 }
        	 // 重置上一状态
        	 preId1 = list[0].id;
        	 preId2 = list[1].id;
        	 preId3 = list[2].id;
        	 
        	 //console.log("preId1: "+preId1+" preId2: "+preId2+" preId3: "+preId3)
            },time);
	   }
   });
}    

function setImg(id, src){
	 //如果waterMarkState=2，那么就要用canvas生成水印
	if(waterMarkState==2){
		//console.log("id: "+id)
		$('#loadingBg'+id).html('<canvas id="canvas'+id+'" width="'+canvas_width+'" height="'+canvas_height+'" ></canvas>');
			var objNow=$('#canvas'+id)[0];
		drawImg(objNow,src,canvas_width,canvas_height,size_scale,markFontSize,waterMark);
//		$('#loadingBg'+id).addClass("curPage");
//		$('#loadingBg'+id).removeClass("otherPage");
	}else if(waterMarkState==1){
		 //console.log("setImg-page:  " + src);
		$('#loadingBg'+id).html('<img alt="" src="'+src+'" id="ViewContainer_BG_"'+id+' class="border  fwr_page_bg_image"/>')		
	}
}

//canvas 部分

//生成水印
function drawImg(canvasobj,imgurl,img_w,img_h,canvas_rate,markSize,addtext){
	var canvas = canvasobj;
	var ctx = canvas.getContext('2d');
  var img = new Image ;
  img.src = imgurl;
  img.onload  = function () {
              ctx.drawImage(img,0,0,img_w,img_h);
//alert('canvas_rate'+canvas_rate+' ;markSize'+markSize)
      // 绘制文字内容
      for (var p = 0; p < 2; p++) {
          for (var q = 0; q < 4; q++) {
              // 绘制文字  注意：：绘制斜体文字 旋转以后会发生位移，所以必须在旋转之后进行位置的调整；
              drawText(ctx,(q * -120)*canvas_rate,-(40 * p)*canvas_rate,((p * 400)*canvas_rate),(((q * 400)+200)*canvas_rate),markSize,addtext)
          }
      }
  }
}

//水印文字样式
function drawText(canvasCtx,tsx,tsy,x,y,canvasFont,textName) {
  var text = textName;
  canvasCtx.save();//保存原来的状态  绘制字体都是需要旋转倾斜  那么之前绘制的图片就要进行状态的保存
  canvasCtx.rotate(-Math.PI/6);//绘制倾斜字体
  canvasCtx.translate(tsx,tsy);//发生位移进行位移的恢复
  // alert(canvasFont)
  canvasCtx.font = canvasFont;
  canvasCtx.fillStyle = "rgba(204,204,204,0.4)";//字体颜色
  canvasCtx.fillText(text, x,y);
  canvasCtx.restore();//状态的恢复
}

	
function firstCanvasLoad(){
	
    cur_page=$("#startpage").val();
    if(cur_page==null || cur_page=="" || cur_page==1){
	      cur_page=1;
	      
	     preId1=0;
	      preId2=1;
	      preId3=2;
    }else{
    	cur_page_index=cur_page-1;
    	
   	 	preId1=cur_page_index-1;
	     preId2=cur_page_index;
	     preId3=cur_page_index+1;
    }
	
	var fid=$("#fid").val();
	$.ajax({
	     url: 'jumpServlet?page='+cur_page_index+"&fid="+fid,
	     type: 'get',
	     datatype: "json",
	     success: function (ret) {     
			 setTimeout(function(){ 
	      var obj = eval('(' + ret + ')');
	      var list = obj.list;
	      var len=list.length;                
	      //console.log(list);
//	      $('.loadingBg').html("");
	     
	      for(var i=0;i<len;i++){
	     	var src=list[i].src;
	     	//console.log(src);
	     	  //alert(src);
			var id=parseInt(list[i].id);
			
			$('#loadingBg'+id).html('<canvas id="canvas'+id+'" width="'+canvas_width+'" height="'+canvas_height+'"></canvas>');
			var objStartNow=$('#canvas'+id)[0];
			//console.log(objStartNow)
			drawImg(objStartNow,src,canvas_width,canvas_height,size_scale,markFontSize,waterMark);
	      }
	      
	      $jspPane.scrollTop((cur_page_index)*($fwr_page_bg_image_h+margin_px));
	      },1000);			 

		   }
	 });
}








